package com.wyp.didishige.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.wyp.didishige.utils.photo.ThumbnailsUtil;
import com.wyp.didishige.R;
import com.wyp.didishige.model.photo.PhotoInfo;
import com.wyp.didishige.utils.ImageLoaderUtil;
import com.wyp.didishige.utils.photo.RotateImageViewAware;
import com.wyp.didishige.utils.photo.UniversalImageLoadTool;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.datatype.BmobFile;

/**
 * Created by horry on 2015/4/29.
 */
public class GridAdapter extends BaseAdapter {
    private List<PhotoInfo> mList;//本地图片
    private List<String> mNetList;//网络图片
    private Context mContext;
    private RemoveListener removeListener;
    private boolean type;//true 本地类型 false 网络图片

    public GridAdapter(Context context, boolean type) {
        super();
        this.mContext = context;
        this.type = type;
        mList=new ArrayList<PhotoInfo>();
        mNetList=new ArrayList<String>();
    }


    public void setmList(List<PhotoInfo> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void setmNetList(BmobFile[] mNetList) {
        List<String> urls = new ArrayList<String>();

        if (mNetList!=null){
            for (int i=0;i<mNetList.length;i++){
                urls.add(mNetList[i].getUrl());
            }
        }
        this.mNetList = urls;
        notifyDataSetChanged();
    }

    public void appendToList(List<PhotoInfo> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void setRemoveListener(RemoveListener removeListener) {
        this.removeListener = removeListener;
    }

    public void remove(int position) {
        mList.remove(position);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (type) {
            return mList.size();
        } else {
            return mNetList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (type) {
            return mList.get(position);
        } else {
            return mNetList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (type){
                convertView = LayoutInflater.from(mContext).inflate(R.layout.image_item, null);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                holder.remove = (ImageView) convertView.findViewById(R.id.remove);
            }else{
                convertView = LayoutInflater.from(mContext).inflate(R.layout.request_image_item, null);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
            }

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (type) {
            PhotoInfo photoInfo = mList.get(position);
            UniversalImageLoadTool.disPlay(ThumbnailsUtil.MapgetHashValue(photoInfo.getImage_id(), photoInfo.getPath_file()),
                    new RotateImageViewAware(holder.image, photoInfo.getPath_absolute()), R.drawable.ic_launcher);
            holder.remove.setVisibility(View.VISIBLE);
            holder.remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (removeListener != null) {
                        removeListener.remove(position);
                    }
                }
            });
        } else {
            String url = mNetList.get(position);
            ImageLoaderUtil.ImageLoaderInstance.displayImage(url,holder.image,ImageLoaderUtil.Thumbnail_Image_Options);
//            ImageLoader.getInstance().displayImage(url, holder.image, ImageLoadOptions.getOptions());
        }

        return convertView;
    }

    public interface RemoveListener {
        public void remove(int position);
    }

    class ViewHolder {
        ImageView image;
        ImageView remove;
    }
}